//contract_query.js @author zlx
const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    checked:true,
    //搜索条件
    contract: {
      depId: '',
      beginSignTime: '',
      endSignTime: '',
      minTotalAmount: '',
      maxTotalAmount: '',
      proId: '',
      partA: '',
      partB: '',
      signAddr: '',
    },
    depList: [],
    //部门是否已选择
    depIsSelect: false,
    //部门选择第几个
    depIndex: 0,
    proList: [],
    //合同类型是否已选择
    proIsSelect: false,
    //合同类型选择第几个
    proIndex: 0,
    //起始日期是否已选择
    beginSignTimeIsSelect: false,
    //终止日期是否已选择
    endSignTimeIsSelect: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
      depList: wx.getStorageSync('baseData')['1'],
      proList: wx.getStorageSync('baseData')['2']
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.clearQueryCondition()
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  //所属部门选择
  bindDepPickerChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      depIsSelect: true,
      depIndex: e.detail.value,
    })
    this.setData({
      'contract.depId': this.data.depList[this.data.depIndex].codeDetail
    })
    // console.log(this.data.contract)
  },

  //起始日期选择
  bindBeginSignTimeChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      beginSignTimeIsSelect: true,
      'contract.beginSignTime': e.detail.value
    })
  },

  //终止日期选择
  bindEndSignTimeChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      endSignTimeIsSelect: true,
      'contract.endSignTime': e.detail.value
    })
  },

  //最小金额输入
  bindMinTotalAmountInput: function (e) {
    this.setData({
      'contract.minTotalAmount': e.detail.value
    })
  },

  //最大金额输入
  bindMaxTotalAmountInput: function (e) {
    this.setData({
      'contract.maxTotalAmount': e.detail.value
    })
  },


  //项目类型选择
  bindProPickerChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      proIsSelect: true,
      proIndex: e.detail.value
    })
    this.setData({
      'contract.proId': this.data.proList[this.data.proIndex].codeDetail
    })
  },

  //第一签约方输入
  bindPartAInput: function (e) {
    this.setData({
      'contract.partA': e.detail.value
    })
  },

  //第二签约方输入
  bindPartBInput: function (e) {
    this.setData({
      'contract.partB': e.detail.value
    })
  },

  //签约地点输入
  bindSignAddrInput: function (e) {
    this.setData({
      'contract.signAddr': e.detail.value
    })
  },

  clearQueryCondition: function () {
    this.setData({
      'contract.depId': '',
      depIsSelect: false,
      'contract.beginSignTime': '',
      beginSignTimeIsSelect: false,
      'contract.endSignTime': '',
      endSignTimeIsSelect: false,
      'contract.minTotalAmount': '',
      'contract.maxTotalAmount': '',
      'contract.proId': '',
      proIsSelect: false,
      'contract.partA': '',
      'contract.partB': '',
      'contract.signAddr': ''
    })
  },

  goContractList: function () {
    if (this.data.contract.endSignTime != '' && this.data.contract.beginSignTime > this.data.contract.endSignTime) {
      wx.showModal({
        title: '系统提示',
        content: '起始日期不能大于结束日期',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    if (this.data.contract.minTotalAmount && !this.data.contract.minTotalAmount.match('^([0-9]|[1-9][0-9]{1,16}|[0-9]\\.[0-9]{0,3}|[1-9][0-9]{1,16}\\.[0-9]{0,3})$')) {
      wx.showModal({
        title: '系统提示',
        content: '金额只能为整数最大17位，小数最大3位的非负数',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    if (this.data.contract.maxTotalAmount && !this.data.contract.maxTotalAmount.match('^([0-9]|[1-9][0-9]{1,16}|[0-9]\\.[0-9]{0,3}|[1-9][0-9]{1,16}\\.[0-9]{0,3})$')) {
      wx.showModal({
        title: '系统提示',
        content: '金额只能为整数最大17位，小数最大3位的非负数',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    if (this.data.contract.minTotalAmount && this.data.contract.maxTotalAmount && this.data.contract.minTotalAmount > this.data.contract.maxTotalAmount) {
      wx.showModal({
        title: '系统提示',
        content: '最小金额不能大于最大金额',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    wx.navigateTo({
      url: '/pages/contract/contract_filter/contract_list?dep_id='+ this.data.contract.depId + '&begin_sign_time=' + this.data.contract.beginSignTime + '&end_sign_time=' + this.data.contract.endSignTime + '&min_total_amount=' + this.data.contract.minTotalAmount + '&max_total_amount=' + this.data.contract.maxTotalAmount + '&pro_id=' + this.data.contract.proId + '&part_a=' + this.data.contract.partA + '&part_b=' + this.data.contract.partB + '&sign_addr=' + this.data.contract.signAddr+'&title='+'查询合同列表'+'&type=1'
    })
  },

})